package com.chen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.constant.LkdConstant;
import com.chen.constant.MessageConstant;
import com.chen.exception.BaseException;
import com.chen.mapper.OrderMapper;
import com.chen.mapper.SkuMapper;
import com.chen.pojo.common.PageResult;
import com.chen.pojo.dto.SkuDto;
import com.chen.pojo.entry.Sku;
import com.chen.pojo.vo.SkuVo;
import com.chen.service.SkuService;
import com.chen.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/17
 */
@Service
@Transactional
public class SkuServiceImpl implements SkuService {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 商品搜索
     *
     * @param pageIndex 起始页
     * @param pageSize  页大小
     * @param skuName   商品名称
     * @return PageResult
     */
    @Override
    public PageResult<SkuVo> searchList(Integer pageIndex, Integer pageSize, String skuName) {
        Page<SkuVo> page = new Page<>(pageIndex, pageSize);
        skuMapper.selectSkuPage(page, skuName);
        return PageUtils.mapperResult(page);
    }

    /**
     * 新增商品
     *
     * @param skuDto 商品信息
     * @return 添加结果
     */
    @Override
    public Boolean addSku(SkuDto skuDto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuDto, sku);
        sku.setSkuId(String.valueOf(System.currentTimeMillis()));
        sku.setCreateTime(LocalDateTime.now());
        sku.setUpdateTime(LocalDateTime.now());
        skuMapper.insert(sku);
        return true;
    }

    /**
     * 更新商品
     *
     * @param skuId  商品id
     * @param skuDto 商品信息
     * @return 修改结果
     */
    @Override
    public Boolean updateSku(String skuId, SkuDto skuDto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuDto, sku);
        sku.setUpdateTime(LocalDateTime.now());
        sku.setSkuId(skuId);
        skuMapper.updateById(sku);
        return true;
    }

    /**
     * 文件上传本地
     *
     * @param fileName 文件
     * @return
     */
    @Override
    public String fileUpload(MultipartFile fileName) throws Exception {
        if (fileName == null || fileName.isEmpty()) {
            throw new BaseException(MessageConstant.FILE_NOT_EXIST);
        }
        String originalFilename = fileName.getOriginalFilename();
        if (originalFilename == null || originalFilename.isEmpty()) {
            throw new BaseException(MessageConstant.FILE_IS_EMPTY);
        }
        String now = LocalDate.now().toString().replace("-", "");
        String path = LkdConstant.ROOT_PATH + now + LkdConstant.SEPARATOR;
        File directory = new File(path);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        // 创建完整的目标文件路径
        File destFile = new File(directory, originalFilename);

        fileName.transferTo(destFile);
        return LkdConstant.UPLOAD_PATH + now + LkdConstant.SEPARATOR + originalFilename;
    }

    /**
     * 获取商圈下销量前10的商品(补货推荐)
     *
     * @param businessId 商圈id
     * @return 结果
     */
    @Override
    public List<SkuVo> getBusinessTop10(Integer businessId) {
       return  orderMapper.selectAmountTop10(businessId);
    }
}
